package com.chris.core.socket.netty.service.http;

import com.chris.core.socket.netty.dto.SocketTypeEnum;
import com.chris.core.socket.netty.handler.WebSocketFrameHandler;
import com.chris.core.socket.netty.util.NettyServerUtil;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.springframework.stereotype.Service;

/**
 * netty websocket server
 */
//@Service
public class NettyWebSocketServerService {
    NettyServerUtil NettyServer  = new NettyServerUtil(10104,180000,"\r\n", SocketTypeEnum.WebSocket) {
        @Override
        public void AddChannelHandler(ChannelPipeline pipeline) {
            //http 基于 http 编码和解吗
            pipeline.addLast(new HttpServerCodec());
            //以块的方式写 ChunkedWriteHandler
            pipeline.addLast(new ChunkedWriteHandler());
            //http分段处理器
            pipeline.addLast(new HttpObjectAggregator(8192));
            //WebSocket协议
            pipeline.addLast(new WebSocketServerProtocolHandler("/chrisws"));
            pipeline.addLast(new WebSocketFrameHandler());
        }
    };

    public NettyWebSocketServerService() throws InterruptedException {
        NettyServer.Open();
    }
}
